package com.yjj.demo.thread.web;

import com.yjj.demo.thread.service.TestService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;

/**
 *
 *
 * @author yangjiajia
 * @time 2021/8/22 11:44
 */
@RestController
@RequestMapping("/test")
public class TestController {

    private Logger logger = LoggerFactory.getLogger(TestController.class);

    @Autowired
    private TestService testService;

    private AtomicInteger id = new AtomicInteger();

    @GetMapping("/get")
    public String get() {
        logger.info("test get...");
        return UUID.randomUUID().toString();
    }

    @GetMapping("/timeout")
    public String timeout() {
        int nextId= id.getAndIncrement();
        try {
            Thread.sleep(3000);
            logger.info("thread name:{},id:{}", Thread.currentThread().getName(), nextId);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        return "id:" + nextId;
    }

    @GetMapping("/task")
    public void task() {
        testService.taskTest();
    }
}
